window.onload = function () {
    document.getElementById("new-role").className = "add_class";
    document.getElementById("system-management").className = "add-nav-active";
    document.getElementById("organizational_management_ul").style.display = "inline-block";
    document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
    initialize_page()           // 初始化页面
};

function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function initialize_page() {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/new_role.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("genre_type=1");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.JSONObject = JsonDate["name_dict"];
                create_model(JsonDate["model_list"])
            }
        }
    };
}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

function create_model(json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");
        td.innerHTML = "<input onclick='create_permission(this.id)' id='"+json_data[i]["modelId"]+"' class='check_box' type=\"checkbox\">";td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["modelName"];td1.title = json_data[i]["modelName"];td1.style.textAlign = "center";
        tr.appendChild(td);tr.appendChild(td1);
        t_table.appendChild(tr);
    }
}

// checkBox不允许多选
function check_more_one_not(ele_id) {
    var check_box_list = document.getElementsByClassName("check_box");
    for (var i=0; i<check_box_list.length; i++){
        if (String(check_box_list[i].id) !== String(ele_id)){
            document.getElementById(check_box_list[i].id).checked = false;
        }else {
            document.getElementById(check_box_list[i].id).checked = true;
        }
    }
}

// 创建权限列表
function create_permission(ele_id) {
    check_more_one_not(ele_id);
    build_jurisdiction();
    var name_list = JSONObject[ele_id];
    var jurisdiction = document.getElementById("jurisdiction");
    jurisdiction.innerHTML = "";
    if (document.getElementById(ele_id).checked){
        for (var i=0; i<name_list.length; i++){
            var div = document.createElement("div");
            div.style.marginLeft = "5%";
            div.style.marginTop = "3%";
            var input = document.createElement("input");
            input.setAttribute("type", "checkbox");
            input.className = "permission_check";
            input.setAttribute("value", name_list[i]);
            div.appendChild(input);
            var label = document.createElement("label");
            label.innerHTML = name_list[i];
            label.style.fontSize = "14px";
            label.style.cursor = "pointer";
            label.style.marginLeft = "3%";
            div.appendChild(label);
            jurisdiction.appendChild(div);
        }
    }
}

// 全选权限
function check_all(select_type) {
    var permission_check_list = document.getElementsByClassName("permission_check");
    if ( select_type === 1){
        for (var i=0; i<permission_check_list.length; i++){
            permission_check_list[i].checked = true;
        }
    }else {
        for (var t=0; t<permission_check_list.length; t++){
            permission_check_list[t].checked = false;
        }
    }

}

// 检查数据是否填写/选择
function check_data() {
    if (document.getElementById("stationName").value){
        document.getElementById("stationName").removeAttribute("style");
        return true
    }else {
        alert("请输入角色名");
        document.getElementById("stationName").style.borderColor = "red";
        return false
    }
}

// 保存权限数据
function save_data() {
    if (check_data()) {
        var stationName = document.getElementById("stationName").value;                 // 角色名
        var permission_check_list = document.getElementsByClassName("permission_check");
        var permission = "";                // 定义权限字符串
        for (var b=0; b<permission_check_list.length; b++) {
            if (permission_check_list[b].checked) {
                permission = permission + permission_check_list[b].value + ","
            }
        }
        if (typeof(window.permission_str) === "undefined"){
            permission = permission.slice(0, -1)
        }else {
            if (window.permission_str.search(permission) === -1) {
                permission = (window.permission_str + permission).slice(0, -1)
            }else {
                permission = window.permission_str.slice(0, -1)
            }
        }
        var position_statement = document.getElementById("position_statement").value;
        var sent_str = "genre_type=2&stationName="+stationName + "&permission=" + permission + "&position_statement=" + position_statement;

        document.getElementById("loading").style.display = "inline-block";

        var xhr = new XMLHttpRequest();
        var local_host = window.location.host;          // 获取当前IP
        xhr.open('post', "/system-setup/new_role.html", true);
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        xhr.send(sent_str);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    document.getElementById("loading").style.display = "none";
                    var JsonDate = JSON.parse(xhr.responseText);
                    window.JSONObject = JsonDate["name_dict"];
                    create_model(JsonDate["model_list"]);
                    document.getElementById("loading").style.display = "none";
                    document.getElementById("stationName").value = "";
                    document.getElementById("position_statement").value = "";
                    document.getElementById("jurisdiction").innerHTML = "";
                }
            }
        };
    }
}

// 构建权限字符串
function build_jurisdiction() {
    var model_name_list = document.getElementsByClassName("check_box");
    var model_ID = "";
    for (var a=0; a<model_name_list.length; a++){
        if (model_name_list[a].checked) {
            model_ID = model_name_list[a].id;
        }
    }
    if (model_ID){
        var permission_check_list = document.getElementsByClassName("permission_check");
        if (typeof(window.permission_str) === "undefined"){
            var permission_str = "";                // 定义权限字符串
        }else {
            permission_str = window.permission_str;                // 定义权限字符串
        }
        for (var b=0; b<permission_check_list.length; b++) {
            if (permission_check_list[b].checked) {
                permission_str = permission_str + permission_check_list[b].value + ","
            }
        }
        if (permission_str){
            window.permission_str = permission_str;
        }
    }
}
